# -*- coding: utf-8 -*-
import scrapy
from ..items import BiqugeItem


class XiuzhenSpider(scrapy.Spider):
    name = 'xiuzhen'

    # allowed_domains = ['xxx']
    # start_urls = ['http://xxx/']

    def start_requests(self):
        for page in range(1, 88):
            yield scrapy.Request(url='http://www.xbiquge.la/fenlei/2_' + str(page) + '.html', callback=self.parse)

    def parse(self, response):
        all_li = response.xpath("//div[@id='newscontent']/div[@class='l']/ul/li")
        for li in all_li:
            name = li.xpath("./span[@class='s2']/a/text()")[0].extract()
            href = li.xpath("./span[@class='s2']/a/@href")[0].extract()
            yield scrapy.Request(url=href, callback=self.parse_info, meta={'name': name})

    def parse_info(self, response):
        name = response.meta['name']
        zuozhe = response.xpath("//div[@id='info']/p[1]/text()")[0].extract()
        dongzuo = response.xpath("string(//div[@id='info']/p[2])")[0].extract().replace('\xa0', '')
        zuihou = response.xpath("//div[@id='info']/p[3]/text()")[0].extract()
        zuixin = response.xpath("//div[@id='info']/p[4]/a/text()")[0].extract()
        jianjie = response.xpath("//div[@id='intro']/p[2]/text()")[0].extract()
        print(name)
        item = BiqugeItem()
        item['name'] = name
        item['zuozhe'] = zuozhe
        item['dongzuo'] = dongzuo
        item['zuihou'] = zuihou
        item['zuixin'] = zuixin
        item['jianjie'] = jianjie
        yield item
